﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using Common;

public partial class website_admin_bbs_topiclist : PageBase
{
    public string token = (new DESCryption()).Encrypt("studentlottery", ConfigurationManager.AppSettings["TokenKey"].Trim());

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;

        PageBegin();

        switch (RequestBase.GetFormString("otype"))
        {
            case "deleteitems":
                Delete();
                break;
            case "approveitems":
                Approve();
                break;  
            case "loadingdata":
                LoadingData();
                break; 
        }

        if (!IsPostBack)
        {
            ColumnListBind(); 
        }
    }

    #region 类别列表数据绑定
    void ColumnListBind()
    {
        DataTable dt = BLL.CommonBLL.Select("BBSColumn", "*", " ORDER BY [Sequence] ASC");

        foreach (DataRow dr in dt.Select(""))
        { 
            selColumnList.Items.Add(new ListItem(dr["Name"].ToString().Trim(), dr["ID"].ToString().Trim()));
        }
    } 
    #endregion

    #region 信息列表
    /// <summary>
    /// 信息列表
    /// </summary>
    void LoadingData()
    {
        try
        {
            StringBuilder SQL = new StringBuilder();

            if (RequestBase.GetFormString("columnid") != "")
            {
                SQL.Append(" AND ColumnId = " + RequestBase.GetFormString("columnid"));
            }

            if (RequestBase.GetFormString("keys") != "")
            {
                SQL.Append(" AND (Title LIKE '%" + RequestBase.GetFormString("keys") + "%' OR UserNickName LIKE '%" + RequestBase.GetFormString("keys") + "%' OR [Content] LIKE '%" + RequestBase.GetFormString("keys") + "%')");
            }

            if (Utils.IsNumber(RequestBase.GetFormString("statuscode")))
            {
                SQL.Append(" AND IsApproved = " + RequestBase.GetFormString("statuscode"));
            }

            Control uc_rep = LoadControl("~/website_admin/bbs/_controls/ctlTopicList.ascx");
            Repeater rptTopicList = (Repeater)uc_rep.FindControl("rptTopicList");
            DBUtility.Pagination pagination = new DBUtility.Pagination();

            if (!string.IsNullOrEmpty(RequestBase.GetString("index")))
                pagination.Sequence = Convert.ToInt32(RequestBase.GetString("index"));
            else
                pagination.Sequence = 1;

            pagination.ItemUnit = "条记录";
            pagination.SortSentence = " ORDER BY PublishTime DESC";
            pagination.Primarykey = "Id";
            pagination.SearchField = "*";
            pagination.TableName = "BBS";
            if (!string.IsNullOrEmpty(RequestBase.GetString("pagesize")))
                pagination.PageSize = Convert.ToInt32(RequestBase.GetString("pagesize"));
            else
                pagination.PageSize = 20;
            pagination.UrlStr = "";
            pagination.ImagesPath = "../_imgs/grid/";
            pagination.SearchSentence = SQL.ToString();

            rptTopicList.DataSource = pagination.GetDataTable();
            rptTopicList.DataBind();

            pagination.PageBarFlexigrid();

            uc_rep.GetType().GetProperty("PaginationBar").SetValue(uc_rep, pagination.PageBar, null); 

            StringBuilder strb = new StringBuilder();
            StringWriter sw = new StringWriter(strb);
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            rptTopicList.RenderControl(htw);

            BLL.AjaxHelper.ResponseWrite(strb.ToString());
        }
        catch (System.Threading.ThreadAbortException err)
        {
            //nothing   to   do   
        }
        catch (Exception err)
        {
            BLL.AjaxHelper.ResponseWrite(err.Message);
        }
    }
    #endregion 

    #region 删除
    /// <summary>
    /// 删除目录
    /// </summary>
    void Delete()
    {
        /// 逻辑删除记录
        string res = "";

        //if (Validate("q_a_admin", 2))
        //{
            try
            {
                List<int> _item = new List<int>();
                string[] arrIds = RequestBase.GetFormString("itemsid").Split(',');
                foreach (string _id in arrIds)
                {
                    _item.Add(int.Parse(_id));
                }

                (new BLL.BBS.BBSBO()).DeleteBSS(_item);
                res = "succeeded";
            }
            catch (Exception err)
            {
                res = err.Message; 
            }
        //}
        //else
        //    res = "当前用户无操作权限！";

        BLL.AjaxHelper.ResponseWrite(res);
    }
    #endregion

    #region 
    /// <summary>
    /// 审核
    /// </summary>
    void Approve()
    {
        /// 逻辑删除记录
        string res = "";

        //if (Validate("q_a_admin", 1))
        //{
            try
            {
                List<int> _item = new List<int>();
                string[] arrIds = RequestBase.GetFormString("itemsid").Split(',');
                foreach (string _id in arrIds)
                {
                    _item.Add(int.Parse(_id));
                }

                (new BLL.BBS.BBSBO()).ApproveBSS(_item, this.SystemUserId, RequestBase.GetFormString("typecode"));
                res = "succeeded";
            }
            catch (Exception err)
            {
                res = err.Message;
            }
        //}
        //else
        //    res = "当前用户无操作权限！";

        BLL.AjaxHelper.ResponseWrite(res);
    }
    #endregion
}
